// 炸弹人对象


class Boomer extends egret.Sprite {

    private man: egret.Bitmap;

    private manStatic: egret.Texture;
    private manRun1: egret.Texture;
    private manRun2: egret.Texture;
    
    private current: Chain;
    private runChain: Chain;

    private runTexture = [];
    public enemy;
    /**
     * 尚未执行的且在排队的移动操作数
     */
    private queueCount = 0;
    private lastExecuteTime;
    private mapData;

    public constructor (x, y, mapData) {
        super()

        this.lastExecuteTime = egret.getTimer();
        this.mapData = mapData;

        this.createMan(x, y)
    }

    public manX() {
        return this.man.x;
    }

    public manY() {
        return this.man.y;
    }
    public createMan(x, y) {
        this.man = new egret.Bitmap();
        this.manStatic = RES.getRes("72_png");
        this.manRun1 = RES.getRes("52_png");
        this.manRun2 = RES.getRes("62_png");

        this.runTexture[0] = this.manRun1;
        this.runTexture[1] = this.manRun2;

        this.man.x = x;
        this.man.y = y;

        this.man.texture = this.manStatic

        this.addChild(this.man);
        return this.man
    }
    
    public moveUp() {
        if(this.man.y <= 0)
            return;
        console.log("执行-");
        // 检测距离上次执行时间差
        let time = egret.getTimer() - this.lastExecuteTime;
        console.log(time);

        if (time < 500) {
            // 排队
            this.queueCount++;
            console.log("排队: " + this.queueCount * (500 - time));
            setTimeout(this._moveUp.bind(this), this.queueCount * (500 - time));
        }else {
            // 不排队
            console.log("不排队");
            this.lastExecuteTime = egret.getTimer();
            this.man.y -= 64;
        }
    }

    public moveDown() {
        if(this.man.y >= 768 - 64)
            return;
        console.log("执行-");
        // 检测距离上次执行时间差
        let time = egret.getTimer() - this.lastExecuteTime;
        console.log(time);

        if (time < 500) {
            // 排队
            this.queueCount++;
            console.log("排队: " + this.queueCount * (500 - time));
            setTimeout(this._moveDown.bind(this), this.queueCount * (500 - time));
            /*
            let timer = new egret.Timer(this.queueCount * (500 - time), 1);
            timer.addEventListener(egret.TimerEvent.TIMER, this.moveDown, this);
            timer.start();*/
        }else {
            // 不排队
            console.log("不排队");
            this.lastExecuteTime = egret.getTimer();
            this.man.y += 64;
        }
    }
    public moveLeft() {
        if(this.man.x <= 0)
            return;
        console.log("执行-");
        // 检测距离上次执行时间差
        let time = egret.getTimer() - this.lastExecuteTime;
        console.log(time);

        if (time < 500) {
            // 排队
            this.queueCount++;
            console.log("排队: " + this.queueCount * (500 - time));
            setTimeout(this._moveRight.bind(this), this.queueCount * (500 - time));
        }else {
            // 不排队
            console.log("不排队");
            this.lastExecuteTime = egret.getTimer();
            this.man.x -= 64;
        }
    }
    public moveRight() {
        if(this.man.x >= 1280 - 64)
            return;
        console.log("执行-");
        // 检测距离上次执行时间差
        let time = egret.getTimer() - this.lastExecuteTime;
        console.log(time);

        if (time < 500) {
            // 排队
            this.queueCount++;
            console.log("排队: " + this.queueCount * (500 - time));
            setTimeout(this._moveRight.bind(this), this.queueCount * (500 - time));
        }else {
            // 不排队
            console.log("不排队");
            this.lastExecuteTime = egret.getTimer();
            this.man.x += 64;
        }
    }
    public isUpWall() {
        if (this.mapData[Math.floor((this.man.y - 64) / 64) * 20 + Math.floor(this.man.x / 64)] === 1)
            return true;
        else return false;
    }
    public isDownWall() {
        if (this.mapData[Math.floor((this.man.y + 64) / 64) * 20 + Math.floor(this.man.x / 64)] === 1)
            return true;
        else return false;
    }
    public isLeftWall() {
        if (this.mapData[Math.floor(this.man.y/ 64) * 20 + Math.floor((this.man.x - 64) / 64)] === 1)
            return true;
        else return false;
    }
    public isRightWall() {
        if (this.mapData[Math.floor(this.man.y/ 64) * 20 + Math.floor((this.man.x + 64) / 64)] === 1)
            return true;
        else return false;
    }
    public _moveUp(ev) {
        if (this.mapData[Math.floor((this.man.y - 64) / 64) * 20 + Math.floor(this.man.x / 64)] === 1)
            return;
        this.lastExecuteTime = egret.getTimer();
        this.man.y -= 64;
        this.queueCount--;
    }
    public _moveDown(ev) {
        if (this.mapData[Math.floor((this.man.y + 64) / 64) * 20 + Math.floor(this.man.x / 64)] === 1)
            return;
        this.lastExecuteTime = egret.getTimer();
        this.man.y += 64;
        this.queueCount--;
    }
    public _moveLeft(ev) {
        if (this.mapData[Math.floor(this.man.y/ 64) * 20 + Math.floor((this.man.x - 64) / 64)] === 1)
            return;
        this.lastExecuteTime = egret.getTimer();
        this.man.x -= 64;
        this.queueCount--;
    }
    public _moveRight(ev) {
        if (this.mapData[Math.floor(this.man.y/ 64) * 20 + Math.floor((this.man.x + 64) / 64)] === 1)
            return;
        this.lastExecuteTime = egret.getTimer();
        this.man.x += 64;
        this.queueCount--;
    }
    public putBoom() {
        let time = egret.getTimer() - this.lastExecuteTime;
        console.log(time);

        if (time < 500) {
            // 排队
            this.queueCount++;
            console.log("排队: " + this.queueCount * (500 - time));
            setTimeout(this._putBoom.bind(this), this.queueCount * (500 - time));
        }else {
            // 不排队
            console.log("不排队");
            this.lastExecuteTime = egret.getTimer();
            this._putBoom();
        }
    }
    public _putBoom() {
        let x = this.man.x;
        let y = this.man.y;
        let ev = new CustomEvent("onDeployBoom", {
            detail: {
                x: x,
                y: y
            }
        });
        window.dispatchEvent(ev);
    }
    public enemyPostion() {
        return [this.enemy.manX() / 64, this.enemy.manY() /64]
    }
    public myPostion() {
        return [this.manX() / 64, this.manY() / 64]
    }
}